﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPMigrationOracleUtility
{
    public partial class MigrationLog : Form
    {
        ClsLog objLog = new ClsLog();
        DataTable dt = new DataTable();

        public string MappingMode { get; set; }

        /// <summary>
        /// Constructor of MigrationLog class
        /// </summary>
        public MigrationLog(string migMode)
        {
            InitializeComponent();
            datePicFrom.MaxDate = DateTime.Today;
            datePicTo.MaxDate = DateTime.Today;
            MappingMode = migMode;
        }

        /// <summary>
        /// Submit Button Click event - To get Records from 'Migration Log' List through ClsLog class.
        /// </summary>
        /// <param name="sender">Event parameter</param>
        /// <param name="e">Event parameter</param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            dt.Clear();     
            if (datePicFrom.Value > datePicTo.Value)
            {
                MessageBox.Show("Invalid Date Selection");
                datePicFrom.Value = DateTime.Today;
            }
            else
            {
                if (rtbnAllLogs.Checked == true)
                {
                    dt = objLog.GetAllRecords(MappingMode);
                    if (dt.Rows.Count > 0)
                    {
                        dgAllLogs.DataSource = dt;
                        dgAllLogs.Visible = true;
                    }
                    else
                    {
                        MessageBox.Show("No Records Found in the Log","Migration Log",MessageBoxButtons.OK);
                        dgAllLogs.Visible = false;
                    }

                }
                else if (rtbnFilter.Checked == true)
                {
                    dt = objLog.GetFilterRecords(datePicFrom, datePicTo,MappingMode);
                    if (dt.Rows.Count > 0)
                    {
                        dgAllLogs.DataSource = dt;
                        dgAllLogs.Visible = true;
                    }
                    else
                    {
                        MessageBox.Show("No Records Found for your search", "Migration Log", MessageBoxButtons.OK);
                        dgAllLogs.Visible = false;
                    }

                }
            }
        }

        /// <summary>
        /// Close the form
        /// </summary>
        /// <param name="sender">Event parameter</param>
        /// <param name="e">Event parameter</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// Enable / Disable DateTimePicker
        /// </summary>
        /// <param name="sender">Event parameter</param>
        /// <param name="e">Event parameter</param>
        private void rtbnAllLogs_Click(object sender, EventArgs e)
        {
            if (rtbnAllLogs.Checked == true)
            {
                datePicFrom.Enabled = false;
                datePicTo.Enabled = false;
            }
        }

        /// <summary>
        /// Enable / Disable DateTimePicker
        /// </summary>
        /// <param name="sender">Event parameter</param>
        /// <param name="e">Event parameter</param>
        private void rtbnFilter_Click(object sender, EventArgs e)
        {
            datePicFrom.Enabled = true;
            datePicTo.Enabled = true;
        }

        /// <summary>
        /// Reset the DateTimePicker and GridView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            
            dgAllLogs.DataSource = "";
            dgAllLogs.Visible = false;
            datePicFrom.Value = DateTime.Today.AddDays(-7);
            datePicTo.Value = DateTime.Today;
        }

        /// <summary>
        /// MigrationLog form Load event, To get all records from 'Migration Log' List through ClsLog class.
        /// </summary>
        /// <param name="sender">Event parameter</param>
        /// <param name="e">Event parameter</param>
        private void MigrationLog_Load(object sender, EventArgs e)
        {
            datePicFrom.Value = DateTime.Today.AddDays(-7);
            datePicTo.Value = DateTime.Today;
            dt = objLog.GetAllRecords(MappingMode);
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("No Records Found in the Log", "Migration Log", MessageBoxButtons.OK);
            }
            else
            {
                dgAllLogs.DataSource = dt;
                dgAllLogs.Visible = true;
            }
            
        }

    }
}
